package com.loong.musicdemo.util.dataDeal;

import android.util.Log;

import com.loong.musicdemo.model.MV;
import com.loong.musicdemo.model.SearchInfo;
import com.loong.musicdemo.model.Singer;
import com.loong.musicdemo.model.Song;
import com.loong.musicdemo.model.SongList;
import com.loong.musicdemo.util.HostUtil;
import com.loong.musicdemo.util.httpUtil.HttpUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class SearchDeal {
    /**
     * 获取歌手
     * @param keywords 搜索关键字
     * @return 歌手集合
     */
    public static List<Singer> getSearchSingers(String keywords) {
        String url = "http://47.112.224.146:3000/search?keywords=" + keywords + "&limit=10&type=100";
        String response = HttpUtil.sendReturnOKHttpRequest(url);
        List<Singer> singerList = new ArrayList<>();
        try {
            JSONObject object = new JSONObject(response);
            JSONObject result = object.getJSONObject("result");
            if (!result.getString("artistCount").equals("0")) {
                JSONArray artists = result.getJSONArray("artists");
                if(artists.length()>=10){
                    for (int i = 0; i < 10; i++) {
                        JSONObject artist = artists.getJSONObject(i);
                        Singer singer = new Singer();
                        singer.setId(artist.getString("id"));
                        singer.setAccountId(artist.getString("accountId"));
                        singer.setName(artist.getString("name"));
                        singer.setBackgroundUrl(artist.getString("picUrl"));
                        singer.setPicUrl(artist.getString("picUrl"));
                        singer.setImg1v1Url(artist.getString("picUrl"));
                        singerList.add(singer);
                    }
                }else {
                    for (int i = 0; i < artists.length(); i++) {
                        JSONObject artist = artists.getJSONObject(i);
                        Singer singer = new Singer();
                        singer.setId(artist.getString("id"));
                        singer.setName(artist.getString("name"));
                        singer.setAccountId(artist.getString("accountId"));
                        singer.setPicUrl(artist.getString("picUrl"));
                        singer.setImg1v1Url(artist.getString("picUrl"));
                        singer.setBackgroundUrl(artist.getString("picUrl"));
                        singerList.add(singer);
                    }
                }

                return singerList;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return singerList;
    }

    /**
     * 根据关键词获取10首歌曲id列表
     * @param keywords 搜索关键字
     * @return 歌曲id结合
     */
    public static List<String> getSearchSongsId(String keywords) {
        String url = "http://47.112.224.146:3000/search?keywords=" + keywords + "&limit=10";
        String response = HttpUtil.sendReturnOKHttpRequest(url);
        List<String> songIdList = new ArrayList<>();
        try {
            JSONObject object = new JSONObject(response);
            JSONObject result = object.getJSONObject("result");
            if (!result.getString("songCount").equals("0")) {
                JSONArray songs = result.getJSONArray("songs");
                for (int i = 0; i < 10; i++) {
                    JSONObject song = songs.getJSONObject(i);
                    songIdList.add(song.getString("id"));
                }
                return songIdList;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return songIdList;
    }

    /**
     * 获取搜索中的歌单
     * @param keywords 搜索关键字
     * @return 歌单集合
     */
    public static List<SongList> getSearchPlayLists(String keywords) {
        String url = "http://47.112.224.146:3000/search?keywords=" + keywords + "&type=1000&limit=5";
        String response = HttpUtil.sendReturnOKHttpRequest(url);
        List<SongList> songLists = new ArrayList<>();
        try {
            JSONObject object = new JSONObject(response);
            JSONObject result = object.getJSONObject("result");
            if (!result.getString("playlistCount").equals("0")) {
                JSONArray playlists = result.getJSONArray("playlists");
                for (int i = 0; i < playlists.length(); i++) {
                    SongList songList = new SongList();
                    JSONObject playlist = playlists.getJSONObject(i);
                    songList.setSongListId(playlist.getString("id"));
                    songList.setName(playlist.getString("name"));
                    songList.setBackgroundUrl(playlist.getString("coverImgUrl"));
                    songList.setPlayCount(playlist.getString("playCount"));
                    songList.setTrackCount(playlist.getString("trackCount"));
                    songList.setSongIds(MusicDeal.getSongOfSongListsId(songList.getSongListId()));
                    songList.setCommentCount(MusicDeal.getPlaylistCommentCount(songList.getSongListId()));
                    List<String> creator = MusicDeal.getPlayListCreatorInfo(songList.getSongListId());
                    songList.setCreatorId(creator.get(0));
                    songList.setCreatorName(creator.get(1));
                    songList.setCreatorAvatarUrl(creator.get(2));
                    songLists.add(songList);
                }
                return songLists;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return songLists;
    }

    /**
     * 获取热搜hot
     * @return 获取热搜榜
     */
    public static List<SearchInfo> getSearchHotDetail() {
        String url = "http://47.112.224.146:3000/search/hot/detail";
        String response = HttpUtil.sendReturnOKHttpRequest(url);
        List<SearchInfo> hotSearches = new ArrayList<>();
        try {
            JSONObject object = new JSONObject(response);
            JSONArray data = object.getJSONArray("data");
            for (int i = 0; i < data.length(); i++) {
                JSONObject hot = data.getJSONObject(i);
                SearchInfo searchInfo = new SearchInfo();
                searchInfo.setSearchWord(hot.getString("searchWord"));
                searchInfo.setContent(hot.getString("content"));
                searchInfo.setScore(hot.getString("score"));
                searchInfo.setIconUrl(hot.getString("iconUrl"));
                hotSearches.add(searchInfo);
            }
            return hotSearches;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    /**
     * 传入搜索关键词可获得搜索建议,移动端数据
     * @param keyword 搜索关键词
     * @return 搜索建议集合
     */
    public static List<String> getSearchSuggestions(String keyword) {
        String url = "http://47.112.224.146:3000/search/suggest?keywords=" + keyword + "&type=mobile";
        String response = HttpUtil.sendReturnOKHttpRequest(url);
        List<String> searchSuggestions = new ArrayList<>();
        try {
            JSONObject object = new JSONObject(response);
            JSONObject result = object.getJSONObject("result");
            if (result.toString().equals("{}")) {
                searchSuggestions.add("暂无数据");
                return searchSuggestions;
            } else {
                JSONArray allMatch = result.getJSONArray("allMatch");
                for (int i = 0; i < allMatch.length(); i++) {
                    JSONObject match = allMatch.getJSONObject(i);
                    searchSuggestions.add(match.getString("keyword"));
                }
                return searchSuggestions;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    /**
     * 根据关键字搜索MV数据
     * @param keywords 关键字
     * @return MV列表
     */
    public static List<MV> getSearchMV(String keywords) {
        String url = HostUtil.getHost() + "/search?keywords=" + keywords + "&type=1004";
        String request = HttpUtil.sendReturnOKHttpRequest(url);
        List<MV> mvs = new ArrayList<>();
        try {
            if (request != null) {
                JSONObject object = new JSONObject(request);
                JSONObject result = object.optJSONObject("result");
                JSONArray array = null;
                if (result != null) {
                    array = result.optJSONArray("mvs");
                }
                int length = 0;//默认无数据
                if (array != null) {
                    length = Math.min(array.length(), 20);//
                }
                for (int i = 0; i < length; i++) {
                    JSONObject mvInfo = array.optJSONObject(i);
                    mvs.add(MvDeal.getMvByJSONObject(mvInfo));
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return mvs;
    }
}
